package project.addGroup;

import org.json.JSONException;
import org.json.JSONObject;
import project.Dao.groupDao;
import project.Dao.UserDao;
import project.Domain.GroupClass;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static java.lang.System.out;

public class addGroup extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了doGet");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了doPost");
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("执行了service");
        try {
            processAEdFile(request, response);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    protected void processAEdFile(HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException {
        request.setCharacterEncoding("UTF-8");
//        获取操作方式增删改
        String leaderID = request.getParameter("leaderID");
        String name = request.getParameter("name");
        String leader_name = request.getParameter("leader_name");
        String property = request.getParameter("property");
        String dueTime = request.getParameter("dueTime");
        String workload = request.getParameter("workload");
        String startTime = request.getParameter("startTime");
//        创建userDao对象操作数据库
        groupDao groupDao = new groupDao();
//        创建JSON对象封装返回信息
        JSONObject json = new JSONObject();
        GroupClass groupClass = new GroupClass(Integer.parseInt(leaderID), name, property, dueTime, workload, startTime);
        //判断金额是否为数字
        out.println("判断金额是否为数字");
        boolean isNumber = true;
        for(int i = 0; i < property.length(); i++){
            if(property.charAt(i) > '9' || property.charAt(i) < '0'){
                isNumber = false;
                break;
            }
        }
        if(isNumber){
            out.println("是数字");
            if(groupDao.addProperty(groupClass.getLeaderID(), groupClass.getName(), leader_name,Integer.parseInt(groupClass.getProperty())))
            {
                out.println("添加资产成功");
                if (groupDao.add(groupClass)) {
                    UserDao userDao = new UserDao();
                    userDao.updateLeaderID(1, Integer.parseInt(leaderID));
                    json.put("result_msg", 1);
                } else{
                    out.println("添加项目失败");
                    json.put("result_msg", 0);
                }
            }
            else {
                out.println("添加资产管理失败");
                json.put("result_msg", 0);
            }
        }
        else {
            json.put("result_msg", 0);
        }
        out.println(json);
        //将响应消息返回页面
        response.setContentType("text/html; charset=UTF-8");
        response.getWriter().print(json);
        response.getWriter().flush();
        response.getWriter().close();
    }
}